<?php if ( ! defined('APPPATH')) exit('No direct script access allowed');
/**
 *      [PinTuXiu] (C)2001-2099 ONightjar.com Pintuxiu.com.
 *      This is NOT a freeware, use is subject to license terms
*/
include_once APPPATH.'modules/social/OAuth_Twitter.php';
class twitter_api{
	
	private $vendor='twitter';
	private $pz;
	protected $oauth;
	protected $client;
	protected $info;
	function __construct(){
		$socialapis = load_api('setting')->getSetting('socialapi');
		$this->info = $socialapis['twitter'];
		$this->pz = &get_instance();
		$this->session = &load_class('Session');
		$this->oauth = new TwitterOAuth($this->info['appkey'],$this->info['appsecret']);
	}

	function getloginpage($state=NULL,$display=NULL){
		$request_token = $this->oauth->getRequestToken($this->info['callback']);
		$this->session->put('oauth_token',$request_token['oauth_token']);
		$this->session->put('oauth_token_secret',$request_token['oauth_token_secret']);
		/* If last connection failed don't display authorization link. */
		switch ($this->oauth->http_code) {
			case 200:
				/* Build authorize URL and redirect user to Twitter. */
				return $this->oauth->getAuthorizeURL($request_token['oauth_token']);
				break;
			default:
				/* Show notification if something went wrong. */
				echo 'Could not connect to Twitter. Refresh the page or try again later.';
		}
		die;
	}

	function get_accesstoken(){
		$oauth_token = $this->session->get('oauth_token');
		$oauth_token_secret = $this->session->get('oauth_token_secret');
		/* Create TwitteroAuth object with app key/secret and token key/secret from default phase */
		$connection = new TwitterOAuth($this->info['appkey'],$this->info['appsecret'], $oauth_token, $oauth_token_secret);
		/* Request access tokens from twitter */
		$access_token = $connection->getAccessToken($_REQUEST['oauth_verifier']);
		/* Save the access tokens. Normally these would be saved in a database for future use. */
		//$_SESSION['access_token'] = $access_token;
		/* Remove no longer needed request tokens */
		$this->session->put('oauth_token','');
		$this->session->put('oauth_token_secret','');
		$this->info['access_token'] = $access_token;
		return $access_token;
	}
	
	function get_userinfo(){
		if(!empty($this->info['access_token'])){
			$access_token = $this->info['access_token'];

			/* Create a TwitterOauth object with consumer/user tokens. */
			$connection = new TwitterOAuth($this->info['appkey'],$this->info['appsecret'], $access_token['oauth_token'], $access_token['oauth_token_secret']);

			/* If method is set change API call made. Test is called by default. */
			$user_profile = $connection->get('account/verify_credentials');
			$userinfo = array();
			$userinfo['uid'] = $user_profile->id;
			$userinfo['screen_name'] = $user_profile->screen_name;
			$userinfo['name'] = $user_profile->name;
			$userinfo['avatar'] = $user_profile->profile_image_url;
			$userinfo['location'] = $user_profile->location;
			$userinfo['description'] = $user_profile->description;
			$userinfo['url'] = $user_profile->url;
			$userinfo['gender'] = 'none';
			return $userinfo;
		}
		return null;
	}
}
